package xyz.chenyongpeng.userconsumerdubbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.chenyongpeng.entity.ZsUser;
import xyz.chenyongpeng.userconsumerdubbo.service.UserService;
import xyz.chenyongpeng.utils.R;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/User")
public class ZsUserController {

    @Autowired
    private UserService userService;

    @PostMapping("/get")
    public R get(@RequestParam(name = "wx_openid",required = true) String wx_openid) {
        if (StringUtils.isEmpty(wx_openid)){
            return R.error(403,"没有登录");
        }
        ZsUser zsUser = userService.get(wx_openid);
        if (zsUser == null){
            return R.error(404, "没有这个用户");
        }
        int coupon_lost = userService.getCouponLostCount(zsUser.getUId().toString());
        Map<String,Object> user = new HashMap<>();
        user.put("u_id", zsUser.getUId());
        user.put("u_nickname", zsUser.getUNickname());
        user.put("u_img", zsUser.getUImg());
        user.put("coupon_lost", coupon_lost);

        Map<String,Object> result = new HashMap<>();
        result.put("data", user);
        return R.ok(result);
    }

    @PostMapping("/signIn")
    public R signIn(@RequestParam(name = "wx_openid",required = true) String wx_openid,@RequestParam(name = "r_id",required = true) String r_id) {
        if (StringUtils.isEmpty(wx_openid)){
            return R.error(403,"没有登录");
        }
        if (StringUtils.isEmpty(r_id)){
            return R.error(500,"缺少参数");
        }
        String u_id = userService.getUid(wx_openid);
        long sign_in_time = System.currentTimeMillis()/1000;
        int si_id = userService.signResource(r_id,u_id,sign_in_time);

        Map<String,Object> result = new HashMap<>();
        result.put("data", si_id);

        return R.ok(result);
    }

    @PostMapping("/authorize")
    public R authorize(@RequestParam(name = "wx_openid",required = true) String wx_openid,
                          @RequestParam(name = "u_name",required = true) String u_name,
                          @RequestParam(name = "u_nickname",required = true) String u_nickname,
                          @RequestParam(name = "u_phone",required = true) String u_phone,
                          @RequestParam(name = "u_img",required = true) String u_img) {
        if (StringUtils.isEmpty(wx_openid)){
            return R.error(500,"缺少参数");
        }
        Map<String,Object> result = new HashMap<>();
        String u_id = userService.saveOrUpdate(wx_openid,u_name,u_nickname,u_phone,u_img);
        if (!StringUtils.isEmpty(u_id)){
            result.put("u_id", u_id);
            return R.ok(result);
        }
        return R.error(500, "");
    }
}
